//
//  GQHLoginView.m
//  Xanthium
//
//  Created by GuanQinghao on 22/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHLoginView.h"
#import "GQHHeader.h"

@interface GQHLoginView () <UITextFieldDelegate> {
    
    UITextField *mobileTextField;
    UITextField *passwordTextField;
}

@end

@implementation GQHLoginView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kClearColor;
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
}

#pragma mark --View
- (void)loadUserInterface {
    
    UIView *containerView = [[UIView alloc] init];
    containerView.layer.cornerRadius = 5.0;
    containerView.layer.masksToBounds = YES;
    containerView.backgroundColor = [UIColor whiteColor];
    [self addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self).with.offset(GQH_MARGIN_SINGLE);
        make.right.mas_equalTo(self).with.inset(GQH_MARGIN_SINGLE);
        make.top.mas_equalTo(self).with.offset(10);
        make.height.mas_equalTo(120);
    }];
    
    UIImageView *phoneImageView = [[UIImageView alloc] init];
    phoneImageView.image = [UIImage imageNamed:@"login_phone"];
    [containerView addSubview:phoneImageView];
    [phoneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.top.and.width.and.height.mas_equalTo(20);
    }];
    
    mobileTextField = [[UITextField alloc] init];
    mobileTextField.placeholder = @"手机号";
    mobileTextField.text = @"";
    mobileTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    mobileTextField.keyboardType = UIKeyboardTypeNumberPad;
    [containerView addSubview:mobileTextField];
    [mobileTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(phoneImageView.mas_right).with.offset(20);
        make.right.mas_equalTo(containerView).with.inset(20);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(phoneImageView);
    }];
    //加载本地存储的用户名
    if (USER_GET_MOBILE)
    {
        mobileTextField.text = USER_GET_MOBILE;
    }
    
    UIView *splitView = [[UIView alloc] init];
    splitView.layer.cornerRadius = 1.0;
    splitView.layer.masksToBounds = YES;
    splitView.backgroundColor = kLightGrayColor;
    [containerView addSubview:splitView];
    [splitView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(containerView).with.offset(20);
        make.right.mas_equalTo(containerView).with.inset(20);
        make.center.mas_equalTo(containerView);
        make.height.mas_equalTo(1);
    }];
    
    UIImageView *passwordImageView = [[UIImageView alloc] init];
    passwordImageView.image = [UIImage imageNamed:@"login_password"];
    [containerView addSubview:passwordImageView];
    [passwordImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.width.and.height.mas_equalTo(20);
        make.top.mas_equalTo(splitView.mas_bottom).with.inset(20);
    }];
    
    passwordTextField = [[UITextField alloc] init];
    passwordTextField.placeholder = @"密码";
    passwordTextField.text = @"";
    passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    passwordTextField.secureTextEntry = YES;
    [containerView addSubview:passwordTextField];
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(passwordImageView.mas_right).with.offset(20);
        make.right.mas_equalTo(containerView).with.inset(20);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(passwordImageView);
    }];
    //加载本地存储的密码
    if (GET_PASS_WORD)
    {
        passwordTextField.text = GET_PASS_WORD;
    }
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.backgroundColor = kWhiteColor;
    loginButton.layer.cornerRadius = 5.0;
    loginButton.layer.masksToBounds = YES;
    loginButton.titleLabel.font = kDefaultFont;
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton setTitleColor:kAppRedColor forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginWithPassword:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:loginButton];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.mas_equalTo(containerView);
        make.top.mas_equalTo(containerView.mas_bottom).with.offset(10);
        make.height.mas_equalTo(44);
    }];
    
    UIButton *forgetButton = [UIButton buttonWithType:UIButtonTypeCustom];
    forgetButton.backgroundColor = [UIColor clearColor];
    forgetButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    forgetButton.titleLabel.font = kSmallerFont;
    [forgetButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [forgetButton addTarget:self action:@selector(forgetPassword:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:forgetButton];
    [forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(loginButton.mas_bottom).with.offset(10);
        make.right.mas_equalTo(loginButton);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    
//    UILabel *quickLoginLabel = [[UILabel alloc] init];
//    quickLoginLabel.font = kDefaultFont;
//    quickLoginLabel.text = @"第三方登录";
//    quickLoginLabel.textColor = kWhiteColor;
//    quickLoginLabel.textAlignment = NSTextAlignmentCenter;
//    [self addSubview:quickLoginLabel];
//    [quickLoginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.centerX.mas_equalTo(self);
//        make.bottom.mas_equalTo(self).with.inset(0.2 * GQH_SCREEN_HEIGHT);
//        make.width.mas_equalTo(150);
//        make.height.mas_equalTo(40);
//    }];
//
//    UIView *leftSplitView = [[UIView alloc] init];
//    leftSplitView.backgroundColor = kWhiteColor;
//    leftSplitView.layer.cornerRadius = 1.0;
//    leftSplitView.layer.masksToBounds = YES;
//    [self addSubview:leftSplitView];
//    [leftSplitView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.width.mas_equalTo(60);
//        make.height.mas_equalTo(2);
//        make.right.mas_equalTo(quickLoginLabel.mas_left);
//        make.centerY.mas_equalTo(quickLoginLabel);
//    }];
//
//    UIView *rightSplitView = [[UIView alloc] init];
//    rightSplitView.backgroundColor = kWhiteColor;
//    rightSplitView.layer.cornerRadius = 1.0;
//    rightSplitView.layer.masksToBounds = YES;
//    [self addSubview:rightSplitView];
//    [rightSplitView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.width.mas_equalTo(60);
//        make.height.mas_equalTo(2);
//        make.left.mas_equalTo(quickLoginLabel.mas_right);
//        make.centerY.mas_equalTo(quickLoginLabel);
//    }];
//
//    UIButton *qqButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [qqButton setImage:[UIImage imageNamed:@"login_quick_qq"] forState:UIControlStateNormal];
//    [qqButton addTarget:self action:@selector(loginWithQQ:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:qqButton];
//    [qqButton mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.width.and.height.mas_equalTo(50);
//        make.bottom.mas_equalTo(self).with.inset(0.1 * GQH_SCREEN_HEIGHT);
//        make.centerX.mas_equalTo(leftSplitView.mas_right);
//    }];
//
//    UIButton *wechatButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [wechatButton setBackgroundImage:[UIImage imageNamed:@"login_quick_wechat"] forState:UIControlStateNormal];
//    [wechatButton addTarget:self action:@selector(loginWithWechat:) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:wechatButton];
//    [wechatButton mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.size.mas_equalTo(qqButton);
//        make.top.mas_equalTo(qqButton);
//        make.centerX.mas_equalTo(rightSplitView.mas_left);
//    }];
}

- (IBAction)loginWithQQ:(UIButton *)sender {
    DLog(@"QQ快速登录");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_loginWithQQ)]) {
        
        [_qh_delegate qh_loginWithQQ];
    }
    
    
    
}

- (IBAction)loginWithWechat:(UIButton *)sender {
    DLog(@"wechat快速登录");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_loginWithWechat)]) {
        
        [_qh_delegate qh_loginWithWechat];
    }
    
    
}

- (IBAction)loginWithPassword:(UIButton *)sender {
    DLog(@"验证用户名和密码 跳转角色页面");
    
    NSString *mobile = mobileTextField.text;
    NSString *password = passwordTextField.text;
    
    if (mobile.length > 1 && password.length > 1) {
        
        if ([mobile qh_isValidMobile]) {
            
            if ([_qh_delegate respondsToSelector:@selector(qh_loginWithMobile:password:)]) {
                
                [_qh_delegate qh_loginWithMobile:mobile password:password];
            }
        } else {
            
            [self showToastWithText:@"手机号格式不正确"];
        }
    } else {
        
        [self showToastWithText:@"请输入手机号或密码"];
    }
}

- (IBAction)forgetPassword:(id)sender {
    DLog(@"忘记密码 跳转验证手机号页面");
    
    if ([_qh_delegate respondsToSelector:@selector(qh_forgetPassword)]) {
        
        [_qh_delegate qh_forgetPassword];
    }
}

@end
